package com.example.it.studyimagerecycle;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import java.util.List;

/**
 * Created by 鲁迅认识的那只猹 on 7/18/2017 10:37 AM.
 * Class Desc:
 * <p>
 * Update
 * Updated by IT on 7/18/2017 10:37 AM
 * Desc:
 */

public class MyPagerAdapter extends PagerAdapter {

    private final Context context; //上下文环境
    private final List<Integer> imgList; //在ViewPager中显示的集合
    private final List<ImageView> imageViewList; //图片资源Id的集合

    public MyPagerAdapter(Context context, List<ImageView> imageViewList, List<Integer> imageIdList) {
        this.context = context;
        this.imgList = imageIdList;
        this.imageViewList = imageViewList;
    }

    //释放占用的资源
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(this.imageViewList.get(position));
    }

    //获取可滑动页面的数量
    @Override
    public int getCount() {
        return imageViewList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    /*
    * 实例化item
    * */
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = this.imageViewList.get(position);

        int id = this.imgList.get(position);

        //加载出指定的图片，这里使用Glide这个开源项目，图片压缩
        Glide.with(this.context).load(id).into(imageView);

        container.addView(imageView);

        return imageView;
    }
}
